<template>
   <div>
    <el-dialog
      :title="info.isAdd ? '规格添加' : '规格修改'"
      :visible.sync="info.isshow"
      @close="close"
    >
      user:{{ user }}
      <div>attrsArr:{{ attrsArr }}</div>
      <el-form :model="user">
        <el-form-item label="规格名称" label-width="120px">
          <el-input v-model="user.specsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="规格属性"
          label-width="120px"
          v-for="(item, index) in attrsArr"
          :key="index"
        >
          <div class="inputAndBtn">
            <el-input v-model="item.val" autocomplete="off"></el-input>
            <el-button
              type="primary"
              v-if="index == 0"
              @click="attrsArr.push({ val: '' })"
              >新增规格属性</el-button
            >
            <el-button type="danger" v-else @click="attrsArr.splice(index, 1)"
              >删除</el-button
            >
          </div>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { formClose } from "../../../mixins";
import { successAlert, errorAlert } from '../../../utils/alert';
import {reqspecsadd, reqspecsinfo, reqspecsedit} from '../../../http/api';
import path from "path";

export default {
  props: ['info'],
    mixins: [formClose],
  data() {
    return {
      //初始化表单数据
      user:{
        specsname:'',
        attrs:'',
        status:1,
      },
      attrsArr:[
        {val:''},
        {val:''},
      ]
    };
  },
  computed: {
    ...mapGetters({}),
  },
  mounted() {},
  methods: {
    ...mapActions({
      reqArr:'specs/reqArr',
    }),
    //清空
   empty() {
  this.user = {
  specsname: "",
  attrs: "",
  status: 1,
};
this.attrsArr = [
  {
    val: "",
  },
  {
    val: "",
  },
];
},
 checkData(){
     return new Promise((resolve,reject) =>{
       if(this.user.specsname ==''){
         errorAlert('规格名称不能为空');
         return;
       }
       if(this.attrsArr.some((item)=>item.val==='')){
         errorAlert('规格属性不能为空')
         return
       }
       if(new Set(this.attrsArr.map((item)=>item.val)).size<this.attrsArr.length){
         errorAlert('规格属性不能重复')
         return;
       }
       resolve();
     })
   },
//点了添加
add(){
  this.checkData().then(()=>{
//处理attrs
this.user.attrs = this.attrsArr.map((item)=>item.val).join(',');
reqspecsadd(this.user).then((res)=>{
  if(res.data.code==200){
    //弹成功
    successAlert(res.data.msg)
    //清内容
    this.empty();
    //弹框消失
    this.info.isshow = false;
    //重新请列表
    this.reqArr()
  }
})
})
  
}
  ,
   //获取一条数据
   getOne(id){
     reqspecsinfo({id:id}).then((res)=>{
       if(res.data.code===200){
         this.user = res.data.list[0];
         this.attrsArr = this.user.attrs.map((item)=>({val:item}))
       }
     })
   },
   //修改
   update(){
     this.checkData().then(()=>{
  this.user.attrs = this.attrsArr.map((item)=>item.val).join('');
     reqspecsedit(this.user).then((res)=>{
       if(res.data.code==200){
         //弹成功
         successAlert(res.data.msg)
         //弹框内容清空
         this.empty();
         //弹框消失
         this.info.isshow = false;
         //重新请求列表
         this.reqArr();
       }
     })
     })
   
   },
  
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
.inputAndBtn {
  display: flex;
  .el-button {
    width: auto;
  }
  .el-input {
    flex: 1;
  }
}
</style>